﻿using QWManage.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace QWManage.Forms
{
    public partial class FrmExtags : Form
    {
        public FrmExtags()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ExtagsConfigInfo extagsConfigInfo = new ExtagsConfigInfo();
            foreach (TreeNode treeNode in tvTags.Nodes)
            {
                if (!treeNode.Checked) continue;
                TagInfo tagInfo = treeNode.Tag as TagInfo;
                string id = tagInfo.ID;
                if (!extagsConfigInfo.TagIds.Contains(id))
                    extagsConfigInfo.TagIds.Add(tagInfo.ID);
            }
            ExtagsConfigMgr.Set(extagsConfigInfo);
            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void FrmDate_Load(object sender, EventArgs e)
        {
            ExtagsConfigInfo extagsConfigInfo = ExtagsConfigMgr.Get();
            tvTags.ShowRootLines = false;
            string tokenA = API.AccessToken.TokenA.GetAccessToken();
            //获取企业标签库  密码用这个 a_nrmHsQtURZNKLKxMGkFTeHLFXpnN6TPd8-lz7xhHM
            API.TagLibrary92117.TagLibraryArgs tagLibraryArgs = new API.TagLibrary92117.TagLibraryArgs();
            tagLibraryArgs.group_id = null;
            tagLibraryArgs.tag_id = null;
            List<TagInfo> tags = API.TagLibrary92117.TagLibraryAPI.Invoke(tokenA, tagLibraryArgs);
            tvTags.CheckBoxes = true;
            tvTags.Nodes.Clear();
            foreach (TagInfo tagInfo in tags)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Tag = tagInfo;
                treeNode.Text = tagInfo.Name;

                if (extagsConfigInfo.TagIds.Contains(tagInfo.ID))
                    treeNode.Checked = true;

                tvTags.Nodes.Add(treeNode);
            }


            //DateConfigInfo dateConfigInfo = DateConfigMgr.Get();
            //dtFrom.Value = dateConfigInfo.From;
            //dtTo.Value = dateConfigInfo.To;
        }

        private void btnChooseAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNode treeNode in tvTags.Nodes)
                treeNode.Checked = true;
        }

        private void btnChooseNone_Click(object sender, EventArgs e)
        {
            foreach (TreeNode treeNode in tvTags.Nodes)
                treeNode.Checked = false;
        }
    }
}
